#include "PositionNeuron.h"
#include "KohonenSynapse.h"
#include <math.h>

using namespace NeuralPlusPlus::Core;
using namespace NeuralPlusPlus::Core::SOM;

NeuralPlusPlus::Core::SOM::PositionNeuron::PositionNeuron( Point coordinate, KohonenLayer *parent )
	{
	Helper::ValidateNotNull(parent, "parent");

	this->Coordinate = coordinate;
	this->Parent = parent;
	this->Input = 0;
	}

void NeuralPlusPlus::Core::SOM::PositionNeuron::Run()
	{
	int sourceSynapsesSize = SourceSynapses.size();
	if (sourceSynapsesSize > 0)
		{
		Input = 0.0;
		for(int i=0;i<sourceSynapsesSize;i++)
			{
			((KohonenSynapse*)SourceSynapses[i])->Propagate();
			}
		}
	Output = sqrt(Input);
	}

void NeuralPlusPlus::Core::SOM::PositionNeuron::Learn( double learningRate )
	{
	double learningFactor = learningRate * NeighborhoodValue;
	int sourceSynapsesSize = SourceSynapses.size();
	for(int i=0;i<sourceSynapsesSize;i++)
		{
		((KohonenSynapse*)SourceSynapses[i])->OptimizeWeight(learningFactor);
		}
	}